﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Threading;
using CPrint.bo;
using CPrint.util;
using Ice;
using Newtonsoft.Json;

namespace CPrint.ice
{
    public class ProxyService : Proxy.WebSocketProxyDisp_
    {
        public ProxyService()
        {
        }

        private BlockingCollection<PrintObject> queue;

        internal BlockingCollection<PrintObject> Queue { get => queue; set => queue = value; }

        public override string business(string method, string req, Current current)
        {
            ApiResponse apiResponse = new ApiResponse();
            apiResponse.Code = 0;
            if ("DoPrint".Equals("method"))
            {
                PrintObject printObject = JsonHelper.jsonToObject<PrintObject>(req);

                Queue.Add(printObject);
            }
            else
            {
                apiResponse.Code = -1;
                apiResponse.Message = "不支持的操作";
            }
            return JsonHelper.toString(apiResponse);
        }
    }

    public class Bootstrap
    {
        private BlockingCollection<PrintObject> queue;

        internal BlockingCollection<PrintObject> Queue { get => queue; set => queue = value; }

        public int main(string[] args)
        {
            try
            {
                PrintMain printMain = new PrintMain();
                printMain.Queue = Queue;
                Thread t = new Thread(printMain.doPrint);
                t.Start();

                using (Ice.Communicator communicator = Ice.Util.initialize(ref args))
                {
                    var adapter =
                        communicator.createObjectAdapterWithEndpoints("ProxyAdapter", "default -h localhost -p 10000");
                    ProxyService proxyService = new ProxyService();
                    proxyService.Queue = Queue;
                    adapter.add(proxyService, Ice.Util.stringToIdentity("ProxyService"));
                    adapter.activate();
                    communicator.waitForShutdown();
                }
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine(e);
                return 1;
            }
            return 0;
        }

        public class PrintMain
        {
            private BlockingCollection<PrintObject> queue;

            internal BlockingCollection<PrintObject> Queue { get => queue; set => queue = value; }

            public void doPrint()
            {
                PrintObject printObject = Queue.Take();
                while (null != printObject)
                {
                    // 根据操作环节匹配打印模板文件  
                   
                    

                    printObject = Queue.Take();
                }
            }
        }



    }
}
